package joney.main.views.report;

import java.awt.Font;
import java.util.Hashtable;

import joney.main.analysis.DateComparison;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.Dataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

public class PieChartView extends TimeRangeChart {

	public static String ID = "joney.piechartview";
	
	
	 protected PieDataset createDataset(int yearb, int yeare) {
	        DefaultPieDataset dataset = new DefaultPieDataset();
	        setPieData(dataset, yearb, yeare);
	        return dataset;        
	    }
	    
	 protected JFreeChart createChart(Dataset dataset) {
	        
	        JFreeChart chart = ChartFactory.createPieChart(
	            "",  // chart title
	            (PieDataset)dataset,             // data
	            false,               // include legend
	            true,
	            false
	        );

	        PiePlot plot = (PiePlot) chart.getPlot();
	        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
	        plot.setNoDataMessage("No data available");
	        plot.setCircular(false);
	        plot.setLabelGap(0.02);
	        return chart;
	    }
	 
	 private void setPieData(DefaultPieDataset dataset, int yearb, int yeare)
	 {
		 Hashtable<String, Double> values = DateComparison.getAllTypeAmount(yearb, yeare);
	        double total = 0;
	        for(Double d: values.values())
	        {
	        	if(d < 0)
	        		total+=(-d);
	        }
	        	
	        	
	       for(String s: values.keySet())
	       {
	    	   double amount = values.get(s);
	    	   if(amount < 0)
	    		   dataset.setValue(s, (-amount)/total);
	       }
	 }

	protected void updateData(int yearb, int yeare) {
		DefaultPieDataset pds =((DefaultPieDataset)dataset);
		pds.clear();
		setPieData(pds, yearb, yeare);
	}

	protected void updateData(String account, int yearb, int yeare) {
		
	}

	

}
